/* OpenStereo Library - A library to support the development of real time stereoscopic applications.
 * Copyright (C) 2007  Pedro Leite.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef sl_viewport_h
#define sl_viewport_h

#include <stdlib.h>

#include <SL/sl.h>
#include "sl_global_variables.h"

static int slAddViewport(SLViewport viewport) {
	if (g_hash_table_lookup(context->viewports, viewport))
		return SL_VIEWPORT_ALREADY_ADDED;
	g_hash_table_insert(context->viewports, viewport, viewport);
	return SL_NO_ERROR;
}

SL_API SLViewport SL_API_ENTRY slCreateViewport(SLuint width, SLuint height) {
	SLViewport viewport = 0;
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return 0;
	}
	if (width == 0 || height == 0) {
		error = SL_INVALID_PARAMETER;
		return 0;
	}
	viewport = (SLViewport) malloc(sizeof(struct _SLViewport));
	if (viewport != 0) {
		viewport->width = width;
		viewport->height = height;
		// call the plugin function
		viewport->stereoData = currentPlugin->allocateStereoData(width, height);
		// TODO save viewport
		if ((error = slAddViewport(viewport))) {
			free(viewport);
			return 0;
		}
		viewport->method = SL_TRUE_COLOR_ANAGLYPH;
		viewport->camera = 0;
		viewport->scene = 0;
	} else {
		error = SL_CANNOT_CREATE_VIEWPORT;
	}
	return viewport;
}

SL_API void SL_API_ENTRY slDestroyViewport(SLViewport viewport) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}
	if (viewport) {
		if (viewport->stereoData) {
			currentPlugin->destroyStereoData(viewport->stereoData);
			viewport->stereoData = 0;
			viewport->width = 0;
			viewport->height = 0;
			viewport->camera = 0;
			viewport->method = -1;
		}
		g_hash_table_remove(context->viewports, viewport);
		free(viewport);
	}
}

SL_API void SL_API_ENTRY slSetDimension(SLViewport viewport, SLuint width, SLuint height) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return;
	}
	if (width == 0 || height == 0) {
		error = SL_INVALID_PARAMETER;
		return;
	}
	viewport->width = width;
	viewport->height = height;
	if (viewport->stereoData)
		currentPlugin->destroyStereoData(viewport->stereoData);
	viewport->stereoData = currentPlugin->allocateStereoData(width, height);
}

SL_API SLuint SL_API_ENTRY slGetWidth(SLViewport viewport) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return 0;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return 0;
	}
	return viewport->width;
}

SL_API SLuint SL_API_ENTRY slGetHeight(SLViewport viewport) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return 0;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return 0;
	}
	return viewport->height;
}

SL_API void SL_API_ENTRY slSetAnaglyphMethod(SLViewport viewport, SLAnaglyphMethod method) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return;
	}
	if (method < SL_TRUE_COLOR_ANAGLYPH || method > SL_OPTIMIZED_ANAGLYPH) {
		error = SL_INVALID_PARAMETER;
		return;
	}
	viewport->method = method;
}

SL_API SLAnaglyphMethod SL_API_ENTRY slGetAnaglyphMethod(SLViewport viewport) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return (SLAnaglyphMethod) -1;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return (SLAnaglyphMethod) -1;
	}
	return viewport->method;
}

SL_API void SL_API_ENTRY slSetCamera(SLViewport viewport, SLCamera camera) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return;
	}
	if (camera == 0) {
		error = SL_INVALID_PARAMETER;
		return;
	}
	viewport->camera = camera;
}

SL_API SLCamera SL_API_ENTRY slGetCamera(SLViewport viewport) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return 0;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return 0;
	}
	return viewport->camera;
}

SL_API void SL_API_ENTRY slSetScene(SLViewport viewport, SLScene scene) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return;
	}
	viewport->scene = scene;
}

SL_API void SL_API_ENTRY slBindViewport(SLViewport viewport) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}
	if (viewport == 0) {
		error = SL_INVALID_VIEWPORT;
		return;
	}
	currentViewport = viewport;
}

SL_API void SL_API_ENTRY slRenderScene(void) {
	if (context == 0 || !context->initialized) {
		error = SL_CONTEXT_NOT_INITIALIZED;
		return;
	}
	if (currentViewport == 0) {
		// TODO verify this error
		error = SL_INVALID_VIEWPORT;
		return;
	}
	if (currentViewport->scene == 0) {
		// TODO verify this error
		return;
	}
	if (currentViewport->camera == 0) {
		// TODO verify this error
		return;
	}
	currentPlugin->renderStereo(currentViewport, currentViewport->scene,
		currentViewport->stereoData);
}

#endif
